package zmee.pur.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: zyj
 * @Date: 2023/9/4 10:07
 * @Description: 项目启动: 点击时通过项目查询招标立项申请单 400万以上的工程施工类、200万以上的其他类 的标书编制中是否集团招标为是
 */
public class SrcProjectIsGroupBidOpPlugin extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        // 寻源项目编号
        fieldKeys.add("billno");
        // 项目编号
        fieldKeys.add("source");
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : dataEntities) {
            // 项目编号
            DynamicObject source = dataEntity.getDynamicObject("source");
            // 寻源项目编号
            String billno = dataEntity.getString("billno");
            QFilter qFilter = new QFilter("billno", QCP.equals, source.getString("billno"));
            // 查询招标立项申请单
            DynamicObjectCollection query = QueryServiceHelper.query("src_demand", "id", new QFilter[]{qFilter});
            if (query == null || query.size() == 0) {
                throw new KDBizException("该项目启动没有找到对应的招标立项申请单,修改标书编制是否集团招标失败！");
            }
            DynamicObject srcDemand = BusinessDataServiceHelper.loadSingle(query.get(0).get("id"), "src_demand");
            DynamicObject biztype = srcDemand.getDynamicObject("biztype");
            if (biztype == null) {
                throw new KDBizException("该项目启动对应的招标立项申请单业务类型为空,修改标书编制是否集团招标失败！");
            }
            // 当业务类型是工程施工类时
            if ("1104".equals(biztype.getString("number"))) {
                // 价税合计
                BigDecimal money = srcDemand.getBigDecimal("zmee_money");
                if (money.compareTo(new BigDecimal("4000000")) >= 0) {
                    // 更新标书编制中是否集团招标为是
                    updateSrcBiddocIsGroupBid(billno);
                }
            } else {
                // 价税合计
                BigDecimal money = srcDemand.getBigDecimal("zmee_money");
                if (money.compareTo(new BigDecimal("2000000")) >= 0) {
                    // 更新标书编制中是否集团招标为是
                    updateSrcBiddocIsGroupBid(billno);
                }
            }
        }
    }

    /**
     * 更新标书编制中是否集团招标为是
     * @param billno 寻源项目编号
     */
    public void updateSrcBiddocIsGroupBid(String billno) {
        // 寻源项目编号
        QFilter qFilter = new QFilter("billno", QCP.equals, billno);
        // 查询标书编制
        DynamicObjectCollection query = QueryServiceHelper.query("src_biddoc", "id, zmee_isgroupbidbing", new QFilter[]{qFilter});
        if (query == null || query.size() == 0) {
            throw new KDBizException("该项目启动没有找到对应的标书编制,修改标书编制是否集团招标失败！");
        }
        DynamicObject srcBiddoc = BusinessDataServiceHelper.loadSingle(query.get(0).get("id"), "src_biddoc");
        srcBiddoc.set("zmee_isgroupbidbing", true);
        SaveServiceHelper.save(new DynamicObject[]{srcBiddoc});
    }
}
